package com.ebt.m.policy.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebt.m.AppInitService;
import com.ebt.m.R;
import com.ebt.m.activity.WebBaseActivity;
import com.ebt.m.commons.buscomponent.listview.BaseListView;
import com.ebt.m.commons.buscomponent.listview.ISectionFactory;
import com.ebt.m.commons.buscomponent.listview.IView;
import com.ebt.m.commons.utils.NetUtils;
import com.ebt.m.data.bean.ProposalListItem;
import com.ebt.m.data.rxModel.api.PolicyAPI;
import com.ebt.m.policy.ActSearchProductsMore;
import com.ebt.m.policy.ActSearchProposalMore;
import com.ebt.m.policy.bean.EventFinishAct;
import com.ebt.m.data.rxModel.apibean.PolicyProposalItem;
import com.ebt.m.data.rxModel.apibean.PolicyUrl;
import com.ebt.m.policy.presenter.SearchPresenter;
import com.ebt.m.proposal_v2.utils.ProposalUtils;
import com.ebt.m.proposal_v2.widget.dialog.EBTProgressDialog;

import org.greenrobot.eventbus.EventBus;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 按照关键字搜索
 *
 * @author Damon.Han
 */
public class SearchListview extends BaseListView<SearchPresenter> {

    private String wd;
    EBTProgressDialog mProgressDialog;

    public SearchListview(Context context) {
        this(context, null, 0);
    }


    public SearchListview(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SearchListview(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mRefreshLayout.setRefreshNeeded(false);
        initProgressDialog();
    }


    @Override
    protected ISectionFactory createViewFactory() {
        return viewType -> {
            switch (viewType) {
                case 0:
                    return new SearchTitle(getContext());
                case 1:
                    return new BrandsProductViewItem(getContext());
                case 2:
                    SearchContentMore searchContentMore = new SearchContentMore(getContext());
                    searchContentMore.setOnClickViewListener(new IView.OnClickListner() {
                        @Override
                        public void onClick(View view, Object... args) {
                            if (args != null && args[0] != null) {
                                String title = (String) args[0];
                                if (title != null && title.contains("产品")) {
                                    goToActivity(ActSearchProductsMore.class);
                                } else {
                                    goToActivity(ActSearchProposalMore.class);
                                }
                            }
                        }

                        @Override
                        public void onLongClick(View view, Object... args) {

                        }
                    });
                    return searchContentMore;
                case 3:
                    SearchProposalItemView itemView = new SearchProposalItemView(getContext());
                    itemView.setOnClickViewListener(onClickListner);
                    return itemView;
            }
            return null;
        };
    }

    private void goToActivity(Class cls) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("data", wd);
        intent.putExtras(bundle);
        intent.setClass(getContext(), cls);
        getContext().startActivity(intent);

    }

    @Override
    protected SearchPresenter createPresenter() {
        return new SearchPresenter(getContext(), this);
    }

    @Override
    public void update(Object... args) {
        super.update(args);
        if (args == null) {
            return;
        }
        wd = (String) args[0];
        mPresenter.loadNew(args);
    }

    @Override
    public void loadNext() {

    }

    @Override
    protected View configEmptyView() {
        View emptyContent = LayoutInflater.from(getContext()).inflate(R.layout.layout_product_search_empty, null);
        ImageView imageView = (ImageView) emptyContent.findViewById(R.id.img_empty);
        TextView textView = (TextView) emptyContent.findViewById(R.id.mession_empty);
        imageView.setBackgroundResource(R.drawable.ic_empty_product);
        textView.setText(R.string.product_search_null1);
        return emptyContent;
    }

    public IView.OnClickListner onClickListner = new IView.OnClickListner() {
        @Override
        public void onClick(View view, Object... args) {
            switch (view.getId()) {
                case R.id.tv_import:
                    importPolicy(args);
                    break;
                case R.id.ll_content:
                    toProposalDetail(args);
                    break;
            }
        }

        private void importPolicy(Object[] args) {
            showProgress(true);
            PolicyProposalItem pp = (PolicyProposalItem) args[0];
            PolicyAPI opportunityApi = AppInitService.getPolicyApi();
            opportunityApi.getPolicyUrl(pp.pickNum)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(result -> {
                        showProgress(false);
                        PolicyUrl url = result.getData();
                        toPolicy(url.url);
                    }, throwable -> {
                        showProgress(false);
                        throwable.printStackTrace();
                        NetUtils.checkHttpException(getContext(), throwable);
                    });
        }

        private void toPolicy(String url) {
            if (!TextUtils.isEmpty(url)) {
                Intent webIntent = new Intent(getContext(), WebBaseActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString(WebBaseActivity.WEB_TYPE_TITLE, "在线投单");
                bundle.putInt(WebBaseActivity.WEB_TYPE_FRAGMENT, WebBaseActivity.FRAGMENT_4NEW_BASE);
                bundle.putBoolean(WebBaseActivity.WEB_TYPE_HIDE_TOOLBAR, true);
                bundle.putString(WebBaseActivity.WEB_TYPE_LINK, url);
                webIntent.putExtras(bundle);
                getContext().startActivity(webIntent);
                EventBus.getDefault().post(new EventFinishAct());
            }
        }

        private void toProposalDetail(Object[] args) {
            PolicyProposalItem pp = (PolicyProposalItem) args[0];
            String applicantName = (String) args[1];
            ProposalListItem p = new ProposalListItem();
            p.status = pp.proposalStatus;
            p.pickNum = pp.pickNum;
            p.applicantName = applicantName;
            p.proposalName = pp.name;//name:建议书名称就是主险名称
            p.previewUrl = pp.previewUrl;
            p.viewUrl = pp.viewUrl;
            ProposalUtils.gotoItemFromProposalList(getContext(), p);
        }

        @Override
        public void onLongClick(View view, Object... args) {

        }
    };

    public void initProgressDialog() {
        mProgressDialog = new EBTProgressDialog(getContext());
        mProgressDialog.setCancelable(false);
        mProgressDialog.setCanceledOnTouchOutside(false);
    }

    @Override
    public void showProgress(boolean show) {
        if (show) {
            mProgressDialog.show();
        } else {
            mProgressDialog.dismiss();
        }
    }
}
